//
//  ICPhotoPickerManager.m
//  ICome
//
//  Created by zhangrongwu on 16/4/18.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICPhotoPickerManager.h"

#import <AssetsLibrary/AssetsLibrary.h>
#import "TZImagePickerController.h"
#import "TZImageManager.h"
#import "ICHeaderFile.h"

@interface ICPhotoPickerManager()<UIImagePickerControllerDelegate,UINavigationControllerDelegate, ICActionSheetViewDelegate>

@property (nonatomic, weak) UIViewController *fromController;
@property (nonatomic, copy) CompelitionBlock completionBlcok;
@property(nonatomic, strong) NSMutableArray *imageArray; //拍照或者相册获取图片
@property(nonatomic, strong) NSMutableArray *imageAsset;
@property (nonatomic, assign)NSInteger maxCount;
@property (nonatomic, strong)ICActionSheetView *actionSheet;

@end
@implementation ICPhotoPickerManager
+ (instancetype)shareInstace {
    static dispatch_once_t once;
    static id sharedInstance;
    dispatch_once (&once, ^{
        sharedInstance = [[ICPhotoPickerManager alloc] init];
    });
    return sharedInstance;
}

//actionSheet提示
- (void)showActionSheetInView:(UIView *)inView
                     maxCount:(NSInteger)maxCount
               fromController:(UIViewController *)fromController
              completionBlock:(CompelitionBlock)completionBlock{
    self.completionBlcok = [completionBlock copy];
    self.fromController = fromController;
    self.maxCount = maxCount;
//    [self.actionSheet showInView:[ICTools lastWindow].rootViewController.view];
    [self.actionSheet showInView:inView];
    return;
}


- (void)ActionSheetView:(ICActionSheetView *)actionSheetView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 0) {
        [self pushImagePickerController];
    } else if (buttonIndex == 1) {
        [self selectPhotoWithSourceTypeCamera];
    }
}


- (void)selectPhotoWithPickSourceType:(PickSourceType)sourceType
                             maxCount:(NSInteger)maxCount
                       fromController:(UIViewController *)fromController
                      completionBlock:(CompelitionBlock)completionBlock {
    self.completionBlcok = [completionBlock copy];
    self.fromController = fromController;
    self.maxCount = maxCount;
    if (sourceType == sourceTypeCamera) {
        [self selectPhotoWithSourceTypeCamera];
    } else if (sourceType == sourceTypePhotoLibrary) {
        [self pushImagePickerController];
    }
}

- (void)selectPhotoWithSourceTypeCamera {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;
    picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    [self.fromController presentViewController:picker animated:YES completion:nil];
}

#pragma mark - UIImagePickerControllerDelegate
// 拍照
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissViewControllerAnimated:YES completion:nil];
    __block UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
    
    if (image && self.completionBlcok) {
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.imageArray removeAllObjects];
                [self.imageArray addObject:image];
                //照片回传
                self.completionBlcok(self.imageArray);
            });
        });
    }
    return;
}

// 取消
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    [[NSNotificationCenter defaultCenter] postNotificationName:@"NotificationProductionVcBack" object:nil];
    [picker dismissViewControllerAnimated:YES completion:^(){
    }];
}

#pragma mark  ===== 选择照片 =====

- (void)pushImagePickerController {
    
    TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:self.maxCount columnNumber:1 delegate:nil pushPhotoPickerVc:YES];
#pragma mark - 四类个性化设置，这些参数都可以不传，此时会走默认设置
    imagePickerVc.isSelectOriginalPhoto = YES;
    
//    if (self.maxCount > 1) {
//        // 1.设置目前已经选中的图片数组
//        imagePickerVc.selectedAssets = _imageAsset; // 目前已经选中的图片数组
//    }
    imagePickerVc.allowTakePicture = YES; // 在内部显示拍照按钮
    
    // 3. 设置是否可以选择视频/图片/原图
    imagePickerVc.allowPickingVideo = NO;
    imagePickerVc.allowPickingImage = YES;
    imagePickerVc.allowPickingOriginalPhoto = NO;
    imagePickerVc.allowPickingGif = NO;
    
    // 4. 照片排列按修改时间升序
    imagePickerVc.sortAscendingByModificationDate = YES;
    
    /// 5. 单选模式,maxImagesCount为1时才生效
    imagePickerVc.showSelectBtn = YES;
    imagePickerVc.allowCrop = NO;
    imagePickerVc.needCircleCrop = NO;
    imagePickerVc.circleCropRadius = 100;
    
    //imagePickerVc.allowPreview = NO;
#pragma mark - 到这里为止
    
    // You can get the photos by block, the same as by delegate.
    // 你可以通过block或者代理，来得到用户选择的照片.
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        [self.imageArray removeAllObjects];
        [self.imageArray addObjectsFromArray:photos];
        self.imageAsset = [NSMutableArray arrayWithArray:assets];
        //照片回传
        self.completionBlcok(self.imageArray);
    }];
    [self.fromController presentViewController:imagePickerVc animated:YES completion:nil];
}

#pragma mark  ===== 懒加载 =====
- (NSMutableArray *)imageArray {
    if (_imageArray == nil) {
        _imageArray = [NSMutableArray array];
    }
    return _imageArray;
}

-(NSMutableArray *)imageAsset {
    if (!_imageAsset) {
        _imageAsset = [[NSMutableArray alloc] init];
    }
    return _imageAsset;
}

-(ICActionSheetView *)actionSheet {
    if (!_actionSheet) {
        _actionSheet = [[ICActionSheetView alloc] initWithTitles:@[@"从相册选取",@"拍照上传"] delegate:self cancelButtonTitle:@"取消"];
    }
    return _actionSheet;
}
@end
